package com.lbc.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lbc.bean.dto.DonateProjectDTO;
import com.lbc.bean.dto.DonateProjectListBackDTO;
import com.lbc.bean.vo.DonateProjectDetailVO;
import com.lbc.bean.vo.DonateProjectVO;
import com.lbc.common.resp.ResponseResult;
import com.lbc.service.DonateProjectService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @author xyl
 * @date 2021-03-18 16:23
 */
@Api(tags = "捐赠项目 管理接口")
@RequestMapping("/donate-project")
@RestController
public class DonateProjectController {

    @Autowired
    private DonateProjectService donateProjectService;

    @RequiresRoles("admin")
    @ApiOperation("添加捐赠项目 , admin")
    @PostMapping("add")
    public ResponseResult add(@RequestBody @Validated DonateProjectDTO donateProjectDTO){
        return ResponseResult.toResponse(donateProjectService.add(donateProjectDTO));
    }

    @RequiresRoles("admin")
    @ApiOperation("编辑捐赠项目 , admin")
    @PutMapping("modify")
    public ResponseResult modify(@RequestBody DonateProjectDTO donateProjectDTO){
        if (ObjectUtils.isEmpty(donateProjectDTO.getDonateProjectId())) {
            return ResponseResult.error("主键不能为空!");
        }
        return ResponseResult.toResponse(donateProjectService.modify(donateProjectDTO));
    }

    @RequiresRoles("admin")
    @ApiOperation("删除捐赠项目 , admin")
    @DeleteMapping("/remove/{donateProjectId}")
    public ResponseResult remove(@PathVariable("donateProjectId") Integer donateProjectId){
        return ResponseResult.toResponse(donateProjectService.remove(donateProjectId));
    }

    @ApiOperation("前台页面展示列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "第几页", dataType = "Integer", paramType = "query",required = true),
            @ApiImplicitParam(name = "pageSize", value = "数量", dataType = "Integer", paramType = "query",required = true),
            @ApiImplicitParam(name = "state", value = "0.已结束 1.进行中 ,不传则全部", dataType = "Integer", paramType = "query")
    })
    @GetMapping("/front/list")
    public ResponseResult<PageInfo<DonateProjectVO>> list(
            @RequestParam Integer pageNum,
            @RequestParam Integer pageSize,
            @RequestParam(required = false) Integer state){
        PageHelper.startPage(pageNum,pageSize);
        return new ResponseResult<>(new PageInfo<>(donateProjectService.listVo(state)));
    }

    @RequiresRoles("admin")
    @ApiOperation("后端页面展示列表")
    @GetMapping("/back/list")
    public ResponseResult<PageInfo<DonateProjectVO>> listBack(@Validated DonateProjectListBackDTO dto){
        PageHelper.startPage(dto.getPageNum(),dto.getPageSize());
        return new ResponseResult<>(new PageInfo<>(donateProjectService.listBackVo(dto)));
    }

    @ApiOperation("前台页面详情展示")
    @GetMapping("/front/{donateProjectId}")
    public ResponseResult<DonateProjectDetailVO> getFrontDetail(@PathVariable("donateProjectId")Integer donateProjectId){
        return new ResponseResult<>(donateProjectService.getDetailVO(donateProjectId));
    }

}
